const {exec} = require('child_process');
const fs = require('fs');
const path = require('path');
const https = require('https'); // 使用 https 模块替代 axios
// 引入 AdmZip 库
let AdmZip;

const configUrl = "https://gitee.com/luckylqm/config/raw/master/new.json";

exports.getSoftwareList = async function getSoftwareList(url) {
    try {
        if (!url) {
            url = configUrl;
        }
        //如果包含 /blob/ 替换成 /raw/
        if (url.includes('/blob/')) {
            url = url.replace('/blob/', '/raw/');
        }
        // 使用 https 模块替代 axios.get
        const response = await new Promise((resolve, reject) => {
            https.get(url, (res) => {
                let data = '';
                res.on('data', (chunk) => {
                    data += chunk;
                });
                res.on('end', () => {
                    resolve(JSON.parse(data)); // 解析 JSON 数据
                });
            }).on('error', (err) => {
                reject(err);
            });
        });
        let data = response;
        //如果有 softwareList key
        if (data.hasOwnProperty('softwareList')) {
            return data.softwareList;
        } else {
            //兼容 scoop 配置文件
            const fileName = path.basename(url, path.extname(url));
            return [{name: fileName, ...data}];
        }
    } catch (error) {
        console.error('Error fetching software list:', error);
        return null;
    }
};